package com.example.servlet;

import com.example.dao.CustomerDao;
import com.example.dao.GuitarDao;
import com.example.dao.PurchaseRecordDao;
import com.example.entity.Customer;
import com.example.entity.Guitar;
import com.example.entity.PurchaseRecord;
import com.google.gson.Gson;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/purchase")
public class PurchaseRecordServlet extends HttpServlet {
    private PurchaseRecordDao purchaseRecordDao = new PurchaseRecordDao();
    private CustomerDao customerDao = new CustomerDao();
    private GuitarDao guitarDao = new GuitarDao();
    private Gson gson = new Gson();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        if ("list".equals(action)) {
            List<PurchaseRecord> records = purchaseRecordDao.findAll();
            List<Customer> customers = customerDao.findAll();
            List<Guitar> guitars = guitarDao.findAll();

            request.setAttribute("records", records);
            request.setAttribute("customers", customers);
            request.setAttribute("guitars", guitars);
            request.getRequestDispatcher("purchase.jsp").forward(request, response);
        } else if ("delete".equals(action)) {
            int customerId = Integer.parseInt(request.getParameter("customerId"));
            int guitarId = Integer.parseInt(request.getParameter("guitarId"));
            purchaseRecordDao.delete(customerId, guitarId);
            response.sendRedirect("purchase?action=list");
        } else if ("recent".equals(action)) {
            Customer customer = (Customer) request.getSession().getAttribute("customer");
            if (customer != null) {
                List<PurchaseRecord> recentRecords = purchaseRecordDao.findRecentByCustomerId(customer.getId(), 5);
                request.setAttribute("recentRecords", recentRecords);
                request.getRequestDispatcher("purchase.jsp").forward(request, response);
            } else {
                response.sendRedirect("login.jsp");
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        if ("add".equals(action)) {
            int customerId = Integer.parseInt(request.getParameter("customerId"));
            int guitarId = Integer.parseInt(request.getParameter("guitarId"));
            double price = Double.parseDouble(request.getParameter("price"));

            PurchaseRecord record = new PurchaseRecord();
            record.setCustomerId(customerId);
            record.setGuitarId(guitarId);
            record.setPrice(price);
            record.setPurchaseTime(new Date());

            purchaseRecordDao.add(record);
            response.sendRedirect("purchase?action=list");
        }
    }
} 